package com.sun.sort.insert;

import java.util.Arrays;

//插入排序
public class insert1 {
    public static void main(String[] args) {
        int[] arr = {101,34,119,-1,2,1};
        ordinary(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void ordinary(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int insertVal = arr[i];
            //arr[1] 前面那个数的下标
            int insertIndex = i - 1;
            //给insertVal找到插入位置
            while (insertIndex >= 0 && insertVal < arr[insertIndex]){
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }
            arr[insertIndex + 1] = insertVal;
        }
    }
}
